#include "GY_StringData.h"

GY_StringData::GY_StringData(const QString &guid, QGraphicsItem *parent)
    :GY_BasicsDataObjects(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("StringData"));
    initializationPropertyInfo();
}

void GY_StringData::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_StringData::updateObjectName;
    m_PropertyInfoMap.insert(ObjectName.propertyName,ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo Value(this);
    Value.propertyName = "Value";
    Value.propertyValue = QString("ABC");
    Value.updateType = Update;
    Value.propertyType = QStringPropertyType;
    Value.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_StringData::updateValue;
    m_PropertyInfoMap.insert(Value.propertyName,Value);
    m_PropertyInfoMap[Value.propertyName].setConnect();

    GY_PropertyInfo ValueCapacity(this);
    ValueCapacity.propertyName = "Value.Capacity";
    ValueCapacity.propertyValue = int(3);
    ValueCapacity.updateType = Update;
    ValueCapacity.propertyType = IntPropertyType;
    ValueCapacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_StringData::updateValueCapacity;
    m_PropertyInfoMap.insert(ValueCapacity.propertyName,ValueCapacity);
    m_PropertyInfoMap[ValueCapacity.propertyName].setConnect();
}

bool GY_StringData::updateValue(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QString>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    int ValueCapacity = m_PropertyInfoMap["Value.Capacity"].propertyValue.toInt();
    //如果字符串长度大于Value.Capacity，Value.Capacity属性扩大至字符串长度
    if(value.toString().length() > ValueCapacity) {
        m_PropertyInfoMap["Value.Capacity"].propertyValue = value.toString().length();
    }
    return true;
}

bool GY_StringData::updateValueCapacity(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    QString str =  m_PropertyInfoMap["Value"].propertyValue.toString();
    //如果长度小于字符串长度，剪切字符串到value长度
    if(value.toInt() < str.length()) {
        m_PropertyInfoMap["Value"].propertyValue = str.mid(0, value.toInt());
    }
    return true;
}
